package com.tarena.lbs.user.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.dao.repository.impl.AdminRepositoryImpl;
import com.tarena.lbs.user.pojo.param.UserGroupParam;
import com.tarena.lbs.user.pojo.param.UserParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.query.UserGroupQuery;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import com.tarena.lbs.user.service.UserGroupService;
import com.tarena.lbs.user.utils.RandomUserName;
import jdk.jshell.execution.Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserGroupServiceImpl implements UserGroupService {

    @Autowired
    private UserGroupRepository userGroupRepository;
    @Override
    public PageResult<UserGroupVO> pageGroupList(UserGroupQuery userGroupQuery) {
//        UserGroupPO po = new UserGroupPO();
//        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
//        Integer id = userPrinciple.getId();
//        AdminPO admin = adminRepositoryImpl.getAdminById(id);
//        po.setBusinessId(admin.getBusinessId());
//        BeanUtils.copyProperties(userGroupQuery,po);
        // 创建一个新的分页结果对象，用于存储最终的分页查询结果
        PageResult<UserGroupVO> userGroupVoPage = new PageResult<>();
        //total objects pageNo pageSize totalPage
        // 调用 adminRepository 的 getPages 方法进行分页查询，获取包含管理员持久化对象的分页信息
        PageInfo<UserGroupPO> pageInfo = userGroupRepository.pageGroupList(userGroupQuery);
        //pageNo pageSize total
        // 将分页信息中的页码设置到分页结果对象中
        userGroupVoPage.setPageNo(pageInfo.getPageNum());
        // 将分页信息中的每页数量设置到分页结果对象中
        userGroupVoPage.setPageSize(pageInfo.getPageSize());
        // 将分页信息中的总记录数设置到分页结果对象中
        userGroupVoPage.setTotal(pageInfo.getTotal());
        // 声明一个存储对象的列表
        List<UserGroupVO> vos = null;
        // 从分页信息中获取持久化对象列表
        List<UserGroupPO> pos = pageInfo.getList();
        // 判断持久化对象列表是否不为空
        if (CollectionUtils.isNotEmpty(pos)){
            vos=pos.stream().map(po->{
                UserGroupVO vo = new UserGroupVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        userGroupVoPage.setObjects(vos);
        return userGroupVoPage;
    }

    @Override
    public void saveGroup(UserGroupParam param) throws BusinessException {
        //1.判断用户组是否存在
        Long count = userGroupRepository.selectByGroupName(param.getGroupName());
        if (count > 0){
            throw new BusinessException("-1", "用户组已存在");
        }
        //2.将param转换为po
        UserGroupPO po = new UserGroupPO();
        BeanUtils.copyProperties(param,po);
        //3.设置必要数据
        po.setCreateAt(new Date());
        po.setReleaseStatus(2);
        //4.调用dao层方法保存
        userGroupRepository.insertGroup(po);
    }

    @Override
    public void deleteGroup(Integer groupId) throws BusinessException {
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        log.info("userPrinciple:{}", userPrinciple);
         if (userPrinciple.getRole() != Roles.ADMIN && userPrinciple.getRole() != Roles.SHOP){
             throw new BusinessException("-1", "非管理员或商家账户不能删除");
         }
        userGroupRepository.deleteGroup(groupId);
    }

    @Override
    public UserGroupVO selectGroupById(Integer groupId) throws BusinessException {
        UserGroupPO po = userGroupRepository.selectGroupById(groupId);
        UserGroupVO vo = new UserGroupVO();
        BeanUtils.copyProperties(po,vo);
        return vo;
    }

}
